// pages/userPersonal/userPersonal.js
const app = getApp()
const WXAPI = require("../../api/newApi")
const WXAPIOLD = require("../../api/api")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1, // 是否显示左上角图标   1表示显示    0表示不显示
      title: '', // 导航栏 中间的标题
      textColor: '#fff', // 标题颜色
      bgColor: '#fff', // 导航栏背景颜色
      isloading: true,
    },
    currentNavtab: 1,
    scroll_height: '', //系统高度
    MarginTop: '',
    topHeight: '',
    navList: [{
        id: 0,
        txt1: '在线客服',
        txt2: '咨询',
        imgsrc: '../../images/Customer-service.png',
        bgColor: 'rgba(255,184,168,0.53)'
      },
      {
        id: 1,
        txt1: '切换商家版',
        txt2: '切换到商家端',
        imgsrc: '../../images/qiehuan.png',
        bgColor: 'rgba(178,252,209,0.53)'
      },
    ],
    redemptionHttp: app.globalData.redemptionHttp,
    userData: {},
    cardInfo:{
      cardNum:'0',

    },
    urlData: {},
    couponCountData: '0',
    token:'',
    jtkCode: '',
    orderNavList:[
      {id:0,img:'./image/daizhifu.png',value:'待支付'},{id:1,img:'./image/yiwancheng.png',value:'已完成'},
      {id:2,img:'./image/daipingjia.png',value:'待评价'},{id:3,img:'./image/wodedingdan.png',value:'我的订单'}
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      statusHeight: app.gettop.height,
      status: app.gettop.status,
      navHeight: app.gettop.navHeight,
      mobbleBottom: app.mobile.mobbleBottom,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.login_dialog = this.selectComponent("#login_dialog");
    var token = wx.getStorageSync('token')
    this.data.token = token
    // this.login_dialog.loginHide()
    if (token == '') {
      // this.login_dialog.loginShow()
      this.setData({
        userData: {},
        "cardInfo.cardNum": 0,
        couponCountData: 0,
      })
    } else {
      this.login_dialog.loginHide()
      this.getuserInfo()
      this.customerCardInfo()
      this.couponCount()
      this.getOrderUrl()
    }
  },
  //选择头像
  onChooseAvatar(e) {
    //e.detail.avatarUrl就是用户选择的头像本地路径
    console.log(e.detail.avatarUrl)
    var data = {
      file: e.detail.avatarUrl
    }
    WXAPIOLD.appUploadFileNew(data, (res) => {
      if (res.code === 200) {
        this.uploadAvatar(res.result.url)
      }
    })
  },
  //头像上传
  uploadAvatar(avatarUrl){
    let data = {
      avatar: avatarUrl
    }
    WXAPIOLD.updateAvatar(data, (res) => {
      if (res.code === 200) {
        setTimeout(()=>{
          wx.showToast({
            title: '修改成功',
            icon:'none'
          })
        },500)
        this.getuserInfo()
      }
    })
  },
  // 订单状态跳转
  orderClick(e){
    let index = e.currentTarget.dataset.id
    if(this.data.token == ''){
      wx.showToast({
        title: '请登录后查看',
        icon:'none'
      })
      return
    }
    wx.navigateTo({
      url: '/userPageC/pages/userPersonal/order/order?index=' + index,
    })
  },
  //登录成功
  loginSuccess() {
    this.data.token = wx.getStorageSync('token')
    this.getuserInfo()
    this.getOrderUrl()
    this.customerCardInfo()
    this.couponCount()
  },
  jumpLogin(){ //登录
    this.login_dialog.loginShow()
  },
  //获取用户信息
  getuserInfo() {
    WXAPIOLD.userInfo('', (res) => {
      if (res.code === 200) {
        app.globalData.nickName = res.result.nickName
        this.setData({
          userData: res.result
        })
      }else{
        this.setData({
          userData: {}
        })
      }
    })
  },
  //获取第三方订单URL
  getOrderUrl() {
    var jtkCode = wx.getStorageSync('jtkCode')
    var data = {
      jtkCode: jtkCode
    }
    WXAPIOLD.jtkOrderUrl(data, (res) => {
      if (res.code === 200) {
        app.globalData.nickName = res.result.nickName
        this.setData({
          urlData: res.result
        })
      }
    })
  },
  //我的资产
  customerCardInfo() {
    WXAPI.customerCardInfo('', (res) => {
      if (res.code === 200) {
        this.setData({
          cardInfo: res.result
        })
      }else{
        this.setData({
          "cardInfo.cardNum": 0
        })
      }
    })
  },
  //我的券包
  couponCount() {
    WXAPI.couponCount('', (res) => {
      if (res.code === 200) {
        this.setData({
          couponCountData: res.result
        })
      }else{
        this.setData({
          couponCountData: 0
        })
      }
    })
  },
  //联系客服
  handleContact() {
    wx.navigateTo({
      url: '/userPageC/pages/userPersonal/contact/contact',
    })
  },
  feedback() {
    wx.showModal({
      content: '此功能正在开发中，敬请期待！',
      confirmText: '我知道了',
      confirmColor: '#FF5800',
      showCancel: false,
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  handleTenants() {
    wx.navigateTo({
      url: '/userPageC/pages/userPersonal/tenants/tenants',
    })
  },
  //账号设置
  handlesetting() {
    if(this.data.token == ''){
      wx.showToast({
        title: '请登录后查看',
        icon:'none'
      })
      return
    }
    wx.navigateTo({
      url: '/userPageC/pages/userPersonal/personal/personal',
    })
  },
  //唤醒扫一扫功能
  getScancode: function () {
    var that = this;
    // 允许从相机和相册扫码
    wx.scanCode({
      success: (res) => {
        console.log(res.result)
        let parameter = res.result.split("code=")[1];
        console.log(parameter)
        let result = JSON.parse(parameter)

        console.log(result)
        console.log("扫一扫参数", result.param)
        console.log("扫一扫类型", result.type)
        if (result.type == 1) {
          wx.navigateTo({
            url: '/userPageA/pages/scan/information/information?param=' + result.param
          })
        } else if (result.type == 2) {
          wx.navigateTo({
            url: '/userPageA/pages/scan/receiving_assets/receiving_assets?param=' + result.param
          })
        } else if (result.type == 4) {
          this.setData({
            qrCodeUrl: result.param,
          })
          this.getParCollectionQrCodeUrl()
        } else if (result.type == 5) { //退卡页面
          wx.navigateTo({
            url: '/userPageB/pages/returnCard/returnCard?param=' + result.param
          })
        } else {
          console.log('else')
        }
      },
      fail: (res) => {
        console.log(res)
      }
    })
  },
  //解析二维码confirmInfo
  getParCollectionQrCodeUrl() {

    var data = {
      qrCodeUrl: this.data.qrCodeUrl,
      // qrCodeUrl:'https://bee.xiaodaiyan.com/pay/uQRxY%2F6Y4pbfVN4%2BbISi5WEG2fcys%2Beg9h2%2B0dfXuU8%3D',
    }
    WXAPIOLD.parCollectionQrCodeUrl(data, (res) => {
      const data = res.result
      console.log("解析二维码", res)
      if (res.code === 200) {
        var merchantId = data.merchantId
        wx.navigateTo({
          // url: '/pages/pay/pay?QRmerchantId='+merchantId,
          url: '/pages/pay/payTransit/payTransit?QRmerchantId=' + merchantId,
        })
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none'
        })
      }
    })
  },
  //第三方订单
  handleSanOrder(e){
    if(this.data.token == ''){
      wx.showToast({
        title: '请登录后查看',
        icon:'none'
      })
      return
    }
    var id = e.currentTarget.dataset.id
    var url = ''
    if(id == 1){
      url = this.data.urlData.zbyOrderUrl
    }else if(id == 2){
      url = this.data.urlData.bigOrderUrl
    }else if(id == 3){
      url = this.data.urlData.cardOrderUrl
    }
    console.log('url',url)
    var arr  = url.split('?')
    var data = arr[1].split("&")
    console.log('url',data)
    var obj = {}
    data.forEach(item =>{
      var str = item.split('=')
      obj[str[0]] = str[1]
    })
    obj = JSON.stringify(obj)
    wx.navigateTo({
      url: '/userPageC/pages/externalH5/externalH5?url='+arr[0]+'&data='+obj+'&type=true'
    });
  },
  //领券中心
  handleRedemption(){
    if(this.data.token == ''){
      wx.showToast({
        title: '请登录后查看',
        icon:'none'
      })
      return
    }
    var obj = {
      time: new Date().getTime(),
      phone: wx.getStorageSync('phone'),
    }
    obj = JSON.stringify(obj)
    wx.navigateTo({
      url: '/userPageC/pages/externalH5/externalH5?url='+this.data.redemptionHttp+'&data='+obj+'&type=true',
    })
  },
  //区块链钱包
  handleWallet() {
    wx.navigateTo({
      url: '/userPageC/pages/userPersonal/walletSize/walletSize',
    })
  },
  jumpCard() { //卡包
    if(this.data.token == ''){
      wx.showToast({
        title: '请登录后查看',
        icon:'none'
      })
      return
    }
    wx.navigateTo({
      url: '/ecCanvas/pages/myShareholderCard/allCard/allCard',
    })
  },
  jumpVouchers(){//我的券列表
    if(this.data.token == ''){
      wx.showToast({
        title: '请登录后查看',
        icon:'none'
      })
      return
    }
    wx.navigateTo({
      url: '/userPageC/pages/vouchersList/vouchersList',
    })
  },
  jumpFb() { //FB
    if(this.data.token == ''){
      wx.showToast({
        title: '请登录后查看',
        icon:'none'
      })
      return
    }
    wx.navigateTo({
      url: '/ecCanvas/pages/blockchainWallet/blockchainWallet',
    })
  },
  jumpmyFmf() { //我的蜂蜜分
    if(this.data.token == ''){
      wx.showToast({
        title: '请登录后查看',
        icon:'none'
      })
      return
    }
    wx.navigateTo({
      url: '/userPageC/pages/userPersonal/myIntegral/myIntegral',
    })
  },
  //提现
  handleWithdrawal() {
    if(this.data.token == ''){
      wx.showToast({
        title: '请登录后查看',
        icon:'none'
      })
      return
    }
    wx.navigateTo({
      url: '/userPageC/pages/userPersonal/withdrawal/withdrawal',
    })
  },
  // 跳转区块链浏览器
  blockchain(){
    wx.navigateTo({
      url: '/userPageB/pages/userMarket/regulatory/regulatory?url=' + app.globalData.blockchainHttp,
    })
  },
  jumpAbout() { //关于我们
    wx.navigateTo({
      url: '/userPageC/pages/userPersonal/about/about',
    })
  },
  jumpAppeal(){ //跳转我要申诉
    wx.navigateTo({
      url: '/userPageB/pages/userMarket/regulatory/regulatory?url=' + app.globalData.applyAppealHttp,
    })
  },
  jumpHelp(){
    wx.navigateTo({
      url: '/userPageA/pages/problem/problem',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.dialog = this.selectComponent("#auth_dialog");
    this.login_dialog = this.selectComponent("#login_dialog");
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
})